/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package simulator.attributes.car;

import simulator.agents.Agent;
import simulator.attributes.Behavior;
import simulator.data.CarData;
import simulator.data.TankData;
import data.events.impl.MoveEvent;
import simulator.services.ClockService;
import simulator.services.RoadService;
import simulator.testsimulator.TestSimulator;

/**
 *
 * @author Eran
 */
public class DriveSegment extends Behavior {
    
    private final CarData carData;
    private final TankData tankData;
    private final RoadService roadService;
    private final ClockService clockService;
    
    public DriveSegment(Agent a) {
        super(a);
        try{
            this.carData = a.getData(CarData.class);
            this.tankData = a.getData(TankData.class);
            this.roadService = a.getService(RoadService.class);
            this.clockService = a.getService(ClockService.class);
        } catch(UnsupportedOperationException e){
            throw new UnsupportedOperationException("Can't instantiate the behavior " + getClass().getSimpleName() + ": " + e.getMessage());
        }
    }

    @Override
    public void behave(String currState) {
        double percentage = roadService.getPercantage(id), tmp = percentage;
//        int consume = clockService.getTicks();
//        System.out.println(String.format("Tick[%d]:: driving road segment ... %f --> %f", consume, percentage, Math.min(percentage + this.carData.getSpeed(), roadService.getPositionOfNextCar(id))));
        
        //try to proceed, considering the cars ahead.
        percentage = Math.min(percentage + this.carData.getSpeed(), roadService.getPositionOfNextCar(id));
        double edgeLen = this.roadService.getEdgeLength(carData.getCurrSource(), carData.getCurrDestination());
        
        double distance = (percentage - tmp)/100.0*edgeLen;
        this.tankData.consume(distance);
        
        MoveEvent moveEvent = new MoveEvent(id, carData.getCurrSource(), carData.getCurrDestination(), roadService.getPercantage(id), percentage);
//        System.out.printf("Move-Event[%d]:: (%s, %s, %f, %f)\n",moveEvent.getId(), moveEvent.getFromNode(), moveEvent.getToNode(), moveEvent.getStartPrecent(), moveEvent.getEndPrecent());
        TestSimulator.eventWriter.writeEvent(TestSimulator.output, moveEvent);
        roadService.setPercantage(id, percentage);
    }
}
